package XynLeen.GraduationProject.Activities;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.youth.xframe.widget.XToast;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import XynLeen.GraduationProject.Bean.ScheduleBean;
import XynLeen.GraduationProject.R;
import XynLeen.GraduationProject.Utils.SingleChooseDialog;
import XynLeen.GraduationProject.view.BaseActivity;
import XynLeen.GraduationProject.view.EditLineItem;
import XynLeen.GraduationProject.view.TitleArrowLineItem;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class Activity_Schedule_Add extends BaseActivity {


    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    @Bind(R.id.matter)
    EditLineItem mMatter;
    @Bind(R.id.neirong)
    EditLineItem mNeirong;
    @Bind(R.id.time)
    TitleArrowLineItem mTime;

    ScheduleBean mWork;
    @Bind(R.id.add)
    Button mAdd;
    @Bind(R.id.type)
    TitleArrowLineItem type;

    @Bind(R.id.ib_navigation_back)
    ImageButton mIbNavigation_back;
    @Bind(R.id.ib_navigation_del)
    ImageButton mIbNavigation_del;
    @Bind(R.id.tv_navigation_label)
    TextView mTvNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        mIbNavigation_del.setVisibility(View.INVISIBLE);

        mWork = (ScheduleBean) getIntent().getSerializableExtra("bean");
        String date = getIntent().getStringExtra("date");
        if (!TextUtils.isEmpty(date)) {
            mTime.setEnabled(false);
            mTime.setDetail(date + " 00:00");
        }
        if (mWork != null) {
            setData(mWork);
            //显示删除键
            mIbNavigation_del.setVisibility(View.VISIBLE);
        }
        mTime.setDetail(formatter1.format(new Date()));


    }

    private void setData(ScheduleBean work) {
        mMatter.setContent(mWork.getTitle());
        mNeirong.setContent(mWork.getContent());
        mTime.setDetail(mWork.getTime());
        type.setDetail(mWork.getType());
        mAdd.setText("修改");
    }

    @OnClick(R.id.type)
    public void type() {
        String s[]={"重要","一般重要","非常重要"};
        SingleChooseDialog.show(this, s, new SingleChooseDialog.OnDialogItemClickListener() {
            @Override
            public void onItemClick(String value, int position) {
                type.setDetail(value);
            }
        });

    }

    @OnClick(R.id.time)
    public void time(View view) {
        // 隐藏软键盘
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        //时间选择器
        TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                mTime.setDetail((formatter1.format(date) + ""));
            }
        }).setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                .build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

    @OnClick(R.id.add)
    public void add() {

        if (TextUtils.isEmpty(mMatter.getContent())) {
            XToast.error("标题不能为空");
            return;
        }

        if (TextUtils.isEmpty(mNeirong.getContent())) {
            XToast.error("内容不能为空");
            return;
        }

        if (TextUtils.isEmpty(type.getTextViewVal())) {
            XToast.error("重要程度不能为空");
            return;
        }

        if (TextUtils.isEmpty(mTime.getTextViewVal())) {
            XToast.error("时间不能为空");
            return;
        }

        ScheduleBean bean = new ScheduleBean();
        bean.setTitle(mMatter.getContent());
        bean.setContent(mNeirong.getContent());
        bean.setTime(mTime.getTextViewVal());
        bean.setType(type.getTextViewVal());
        if (mWork != null) {
            bean.update(mWork.getId());
        } else {
            bean.save();
        }
        finish();
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_schedule_add;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initView() {

    }


    @OnClick({R.id.ib_navigation_back,R.id.ib_navigation_del})
    public void OnClick(View v){
        switch (v.getId()){
            case R.id.ib_navigation_back:
                finish();
                break;
            case R.id.ib_navigation_del:
                final AlertDialog dialog = new AlertDialog.Builder(this)
                        .setTitle("提示")//设置对话框的标题
                        .setMessage("确定删除吗？")//设置对话框的内容
                        //设置对话框的按钮
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DataSupport.delete(ScheduleBean.class, mWork.getId());
                                finish();
                                dialog.dismiss();
                            }
                        }).create();

                dialog.show();
                break;
        }
    }

}
